<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Gateway for the roles
 *
 * @category RedCross
 * @package Core_Gateway
 */
class Core_Gateway_Role extends RedCross_Gateway_GatewayAbstract
{
	public function __construct()
	{
		$this->_class = 'Role';
		$this->_modelPath = 'Core_Model';
	}

	public function fetchByTemplateId($id)
	{
		$record = Doctrine_Query::create()
		    ->select('st.role_id, st.number, r.name')
			->from($this->getModelClass() . ' r')
			->leftJoin('r.ShiftTemplateRole st')
			->where('st.shift_template_id = ?', $id)
			->fetchArray();

		return $record;
	}
	
	public function fetchByShiftId($shiftId)
	{
		$record = Doctrine_Query::create()
			->select('sr.id, sr.shift_id, sr.leader, r.id, r.name, m.id, m.first_name, m.last_name')
			->from('Core_Model_ShiftRole sr')
			->leftJoin('sr.Member m')
			->leftJoin('sr.Role r')
			->where('sr.shift_id = ?', $shiftId)
			->orderBy('r.sort_order')
			->fetchArray();
		return $record;
		
	}
	
	public function fetchAllSorted()
	{
		$query = Doctrine_Query::create()
			->select('r.id, r.name, r.sort_order')
			->from('Core_Model_Role r')
			->orderBy('r.sort_order');

		return $query->fetchArray();
	}	

}